package com.ruoyi.controller.achievements;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.service.achievements.AcvDepManageService;
import com.ruoyi.service.achievements.AcvTemplateManageService;
import com.ruoyi.util.BaseResult;
import com.ruoyi.util.ExcelUtils;
import com.ruoyi.util.ResultEnum;
import com.ruoyi.util.StringUtil;
import net.sf.jsqlparser.statement.replace.Replace;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author zeo
 * @explain 绩效模板管理
 * @date 2020/8/22 8:55
 */
@Controller
@RequestMapping("/acvTemplateManage")
public class AcvTemplateManageController extends BaseController {

    @Autowired
    AcvTemplateManageService acvTemplateManageService;

    @Autowired
    AcvDepManageService acvDepManageService;

    private String prefix = "achievements/templatemanage";

    private static List<Map<String, Object>> templateList = new ArrayList<Map<String, Object>>();

    @GetMapping()
    public String acvEmpManage(ModelMap mmap) {
        mmap.put("secondaryDeptList", acvDepManageService.findSecondaryDept());
        mmap.put("jobList", acvTemplateManageService.findJobList());
        return prefix + "/templateManage";
    }

    @RequestMapping("/acvTemplateList")
    @ResponseBody
    public TableDataInfo acvTemplateList(String jobId) {
        if (!jobId.equals("") && jobId != null) {
            return getDataTable(acvTemplateManageService.findTemplateList(jobId));
        }
        return getDataTable(templateList);
    }

    @RequestMapping("/importTemplateFile")
    @ResponseBody
    public AjaxResult importTemplateFile(MultipartFile file) throws Exception {
        List<Map<String, Object>> excelData = ExcelUtils.getExcelData(file, new String[]{"assessmentIndex", "describe", "goal", "standard", "distribution"});
        for (Map map : excelData) {
            if(map.get("distribution") != null){
                map.put("distribution", map.get("distribution").toString().replace(".0", ""));
            }
        }
        templateList = excelData;
        return toAjax(1);
    }

    /**
     * 下载模板
     */
    @RequestMapping("/downloadTemplate")
    @ResponseBody
    public AjaxResult downloadTemplate() {
        return acvTemplateManageService.downloadTemplate();
    }

    @RequestMapping("/templateListIsNull")
    @ResponseBody
    public BaseResult templateListIsNull() {
        if (templateList.size() == 0) {
            return BaseResult.build(ResultEnum.FAILURE);
        } else {
            return BaseResult.build(ResultEnum.SUCCESS);
        }
    }

    @RequestMapping("/findJobCount")
    @ResponseBody
    public BaseResult findJobCount(String jobName,String dept) {
        int count = acvTemplateManageService.findJobCount(jobName,dept);
        if (count > 0) {
            return BaseResult.build(ResultEnum.FAILURE);
        } else {
            return BaseResult.build(ResultEnum.SUCCESS);
        }
    }


    @RequestMapping("/TemplateEmpty")
    @ResponseBody
    public BaseResult TemplateEmpty() {
        if (templateList.size() == 0) {
            return BaseResult.build(ResultEnum.FAILURE);
        } else {
            templateList.clear();
            return BaseResult.build(ResultEnum.SUCCESS);
        }
    }

    @GetMapping("/importOption")
    public String importOption(ModelMap mmp) {
        mmp.put("findAllDeptList", acvDepManageService.findAllDept());
        mmp.put("jobList", acvTemplateManageService.findJobList());
        return prefix + "/importOption";
    }

    @RequestMapping("/insertTemplateList")
    @ResponseBody
    public AjaxResult insertTemplateList(int option, String jobName, String jobId,String dept) {
        return toAjax(acvTemplateManageService.insertTemplateList(option, jobName, jobId, templateList,dept));
    }
}
